<?php

/** 
 * 学生消费记录统计模型(mongo)
 * @property User_order_mdl $order
 */
class Tongji_user_mdl extends Mongo_Model {
	public function __construct() {
		$this->collName = 'tongji_user_pay';
		parent::__construct ();
	}
	
	public function getUserTongji($fields=array(),$where=array(),$sort=array(),$sortKey){
		
		$lists = $this->coll->find($where,$fields)
							->sort($sort);
		$lists =  $this->toArray($lists);
		
		$rst = array();
		
		if( !empty($lists) ){
			foreach ($lists as $k=>$v){
				$rst[$v['uid']]['ctime'] = $where['ctime']['$gte'];
				if ( isset($where['ctime']['$lte']) ){
					$rst[$v['uid']]['ctime'] .= ' - '.$where['ctime']['$lte'];
				}
				$rst[$v['uid']]['name'] = $v['name'];
				$rst[$v['uid']]['uid'] = $v['uid'];
				$rst[$v['uid']]['sid'] = $v['sid'];
				$rst[$v['uid']]['sname'] = $v['sname'];
				$rst[$v['uid']]['price_count'] = (int)(isset($rst[$v["uid"]]['price_count'])?($rst[$v["uid"]]['price_count']+$v['price_count']):$v['price_count']);
				$rst[$v['uid']]['usercount'] = (int)(isset($rst[$v["uid"]]['usercount'])?($rst[$v["uid"]]['usercount']+$v['usercount']):$v['usercount']);
				$rst[$v['uid']]['take_sec'] = (int)(isset($rst[$v["uid"]]['take_sec'])?($rst[$v["uid"]]['take_sec']+$v['take_sec']):$v['take_sec']);
				$rst[$v['uid']]['t_1'] = (int)(isset($rst[$v["uid"]]['t_1'])?($rst[$v["uid"]]['t_1']+$v['t_1']):$v['t_1']);
				$rst[$v['uid']]['t_2'] = (int)(isset($rst[$v["uid"]]['t_2'])?($rst[$v["uid"]]['t_2']+$v['t_2']):$v['t_2']);
				$rst[$v['uid']]['t_3'] = (int)(isset($rst[$v["uid"]]['t_3'])?($rst[$v["uid"]]['t_3']+$v['t_3']):$v['t_3']);
			}
			
			//排序
			$rst = $this->array_paixu($rst,$sortKey);	
			
		}
		
		return $rst;
	}

	public function array_paixu($list_scene,$key='price_count',$desc='desc'){
		if($desc=='desc'){
			array_multisort(array_column($list_scene,$key),SORT_DESC,$list_scene);
		}else{
			array_multisort(array_column($list_scene,$key),SORT_ASC,$list_scene);
		}
		return $list_scene;
	}
	
}

